package com.yanqu.road.dao.impl.activity.drink;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.drink.config.DrinkOrderConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DrinkOrderConfigDaoImpl extends TempDao {

    private static String getDrinkOrderConfigSql = "select * from `t_s_activity_drink_order` where `activityId`=?";

    public Map<Integer, DrinkOrderConfig> getDrinkOrderConfigMap(int activityId) {
        Map<Integer, DrinkOrderConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDrinkOrderConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DrinkOrderConfig drinkOrderConfig = new DrinkOrderConfig();
                drinkOrderConfig.setActivityId(rs.getInt("activityId"));
                drinkOrderConfig.setOrderId(rs.getInt("orderId"));
                drinkOrderConfig.setName(rs.getString("name"));
                drinkOrderConfig.setBlockList(StringUtils.stringToIntegerList(rs.getString("blockList"), ";"));
                drinkOrderConfig.setMulti(rs.getInt("multi"));
                drinkOrderConfig.setNumList(StringUtils.stringToIntegerList(rs.getString("numList"), ";"));
                drinkOrderConfig.setMaxTimes(rs.getInt("maxTimes"));
                drinkOrderConfig.setReward(rs.getString("reward"));
                drinkOrderConfig.setSpecParam(rs.getString("specParam"));
                drinkOrderConfig.setWeight(rs.getInt("weight"));
                drinkOrderConfig.setNpcId(rs.getInt("npcId"));
                drinkOrderConfig.setNpcName(rs.getString("npcName"));
                drinkOrderConfig.setNpcDesc(rs.getString("npcDesc"));
                map.put(drinkOrderConfig.getOrderId(), drinkOrderConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
